package com.gameley.dmgtpf.kernel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gameley.dmgtpf.common.exception.MyRuntimeException;
import com.gameley.dmgtpf.common.utils.LoggerUtil;
import com.gameley.dmgtpf.common.utils.PubEnvUtil;
import com.gameley.dmgtpf.common.utils.StreamUtil;
import com.gameley.dmgtpf.kernel.dao.MobileDao;
import com.gameley.dmgtpf.kernel.entity.MobileEntity;
import com.gameley.dmgtpf.kernel.service.MobileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service("mobileService")
public class MobileServiceImpl extends ServiceImpl<MobileDao, MobileEntity> implements MobileService
{
    private List handleRest(List<MobileEntity> entitys)
    {
        List<HashMap<String, Object>> restList = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < PubEnvUtil.getSize(entitys); i++)
        {
            MobileEntity entity = entitys.get(i);
            HashMap<String, Object> restMap = new HashMap<>();
            restMap.put("id", entity.getId());
            restMap.put("name", entity.getName());
            restMap.put("series", entity.getSeries());
            restMap.put("type", entity.getType());
            
            restList.add(restMap);
            
        }
        return restList;
    }
    
    @Override
    public List<?> getSeries(String[] names, String[] prices)
    {
        QueryWrapper<MobileEntity> wrapper = new QueryWrapper<MobileEntity>();
        if (PubEnvUtil.getSize(names) > 0)
        {
            wrapper.in("name", names);
        }
        if (PubEnvUtil.getSize(prices) > 0)
        {
            wrapper.in("price", prices);
        }
        wrapper.orderByAsc("id");
        
        List<MobileEntity> regionEntities = list(wrapper);
        return handleRest(regionEntities);
    }
    
    @Override
    public List<?> getMobileBrands()
    {
        QueryWrapper<MobileEntity> wrapper = new QueryWrapper<MobileEntity>();
        wrapper.orderByAsc("id");
        
        List<MobileEntity> entities = list(wrapper);
        
        return handleRest(entities);
    }
    
    @Override
    public String file2tableSavaDB(MultipartFile file)
    {
        String msg = "";
        int id = 10001;
        ArrayList<MobileEntity> entityList = new ArrayList<MobileEntity>();
        try
        {
            if (PubEnvUtil.isEmptyObj(file))
            {
                throw new MyRuntimeException("操作失败，文件不能为空！");
            }
            String str = StreamUtil.InputStreamTOString(file.getInputStream());
            if (PubEnvUtil.isEmpty(str))
            {
                throw new MyRuntimeException("操作失败，文件内容不能为空！");
            }
            // 华为@Mate系列*华为Mate 40E^2021年3月10日%华为Mate X2^2021年2月22日%
            String[] crleStr = str.split(PubEnvUtil.CRLF);
            for (int i = 0; i < PubEnvUtil.getSize(crleStr); i++)
            {
                String[] strs = crleStr[i].split(PubEnvUtil.AT);
                String name = strs[0].trim();
                msg = name;
                String[] pipes = strs[1].split(PubEnvUtil.COLON);
                
                // 华为@|Mate系列#华为Mate 40E;2021年3月10日%
                if (PubEnvUtil.getSize(pipes) > 1)
                {
                    for (int j = 0; j < PubEnvUtil.getSize(pipes); j++)
                    {
                        String strTemp = pipes[j];
                        if (PubEnvUtil.isEmpty(strTemp)) continue;
                        String[] series = strTemp.split(PubEnvUtil.HASH);
                        String serie = series[0].trim();
                        
                        String[] types = series[1].split(PubEnvUtil.PERCENT);
                        for (int k = 0; k < PubEnvUtil.getSize(types); k++)
                        {
                            String type = types[k].replace(name, "").trim();
                            MobileEntity entity = new MobileEntity();
                            entity.setId(id);
                            entity.setName(name);
                            entity.setSeries(serie);
                            String[] timemarkes = type.split(PubEnvUtil.SEMICOLON);
                            if (PubEnvUtil.getSize(timemarkes) > 1)
                            {
                                entity.setTimemarke(timemarkes[1]);
                            }
                            entity.setType(timemarkes[0]);
                            entity.setPrice(PubEnvUtil.WAVE);
                            entityList.add(entity);
                            id++;
                            // save(entity);
                        }
                    }
                }
                else
                {
                    // 海信@海信A5Pro%海信双屏手机A6%海信H11%
                    String[] types = strs[1].split(PubEnvUtil.PERCENT);
                    for (int j = 0; j < PubEnvUtil.getSize(types); j++)
                    {
                        String type = types[j].replace(name, "").trim();
                        MobileEntity entity = new MobileEntity();
                        entity.setId(id);
                        entity.setName(name);
                        entity.setSeries(type);
                        entity.setType(type);
                        
                        entityList.add(entity);
                        id++;
                        // save(entity);
                    }
                }
                
            }
            saveBatch(entityList);
        }
        catch (Exception e)
        {
            LoggerUtil.getInstance().exception(e);
            System.err.println(e);
            System.err.println(msg);
            System.err.println(id);
        }
        
        return PubEnvUtil.getSuccessMsg("操作成功！！");
    }
}
